package com.xcdh.ittools.utils;

import com.sun.istack.internal.NotNull;
import com.xcdh.ittools.secure.AbstractFieldValidate;
import com.xcdh.ittools.secure.Validate;
import lombok.SneakyThrows;
import org.apache.ibatis.ognl.Ognl;

public class AssertUtil {
    //AssertUtil.simpleValidte(user , {key: 'username', msg: 'username不能为空'} ,  {key: 'age', msg: 'age不能为空'})
    //AssertUtil.simpleValidte(collection ,{key: 'name', msg: 'name不能为空'}, {key: 'degree', msg: 'degree不能为空'} )

    @SneakyThrows
    public static void validate(@NotNull Object target , AbstractFieldValidate...validates){
        for (AbstractFieldValidate validate : validates) {
           boolean retVal = validate.validate(Ognl.getValue(validate.getFieldName() , target)) ;
           if(!retVal){
               throw new RuntimeException(validate.getMessage()) ;
           }
        }
    }
}
